package com.penghaisoft.microwms.materialhand.web.insidewarehouse.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.dto.RawContractChangePlanDto;
import com.penghaisoft.microwms.common.dto.RawCountingDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.RawContractChange;
import com.penghaisoft.microwms.common.wms.model.RawContractChangePlan;
import com.penghaisoft.microwms.common.wms.model.RawCountingDifference;
import com.penghaisoft.microwms.common.wms.model.RawCountingRecords;
import com.penghaisoft.microwms.common.wms.model.RawCountingTask;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.materialhand.web.counting.dao.RawCountingDifferenceMapper;
import com.penghaisoft.microwms.materialhand.web.counting.dao.RawCountingRecordsMapper;
import com.penghaisoft.microwms.materialhand.web.counting.dao.RawCountingTaskMapper;
import com.penghaisoft.microwms.materialhand.web.insidewarehouse.service.IMaterialHandInsideWhService;
import com.penghaisoft.microwms.materialhand.web.storage.dao.RawContractChangeMapper;
import com.penghaisoft.microwms.materialhand.web.storage.dao.RawContractChangePlanMapper;
import com.penghaisoft.microwms.materialhand.web.storage.dao.RawStorageSnMapper;

/** 
* @ClassName: MaterialHandInsideWhServiceImpl 
* @Description: 手持库内服务
* @author jzh
* @date 2018年5月25日 下午4:50:28  
*/
@Service
public class MaterialHandInsideWhServiceImpl extends BaseService implements IMaterialHandInsideWhService {

	 @Autowired
	 private RawCountingTaskMapper rawCountingTaskMapper;
	 
	 @Autowired
	 private RawStorageSnMapper rawStorageSnMapper;

	 @Autowired
	 private RawCountingRecordsMapper rawCountingRecordsMapper;
	 
	 @Autowired
	 private RawCountingDifferenceMapper rawCountingDifferenceMapper;
	 
	 @Autowired
	 private RawContractChangePlanMapper rawContractChangePlanMapper;
	 
	 @Autowired
	 private RawContractChangeMapper rawContractChangeMapper;
	 
	/* @Title: queryRawCountingTask 
	 * @Description: 查询盘点单号是否存在
	 * @author jzh
	 * @date 2018年5月25日 下午4:52:17 
	 */
	@Override
	public List<RawCountingTask> queryRawCountingTask(RawCountingTask rawCountingTask) {
		// TODO Auto-generated method stub
		return rawCountingTaskMapper.queryByAny(rawCountingTask);
	}

	/* @Title: updateRawCountingTask 
	 * @Description: 将盘点任务状态置为盘点中
	 * @author jzh
	 * @date 2018年5月28日 上午10:18:53 
	 */
	@Override
	public Resp updateRawCountingTask(RawCountingTask rawCountingTask) {
		rawCountingTask.setStauts("1");
		Integer updateCnt = rawCountingTaskMapper.update(rawCountingTask);
		if(updateCnt.intValue() == 1){
			return success();
		}else{
			return fail("更改状态失败");
		}
	}

	@Override
	public List<RawCountingTask> queryRawCountingTaskByCode(String countingCode) {
		
		return rawCountingTaskMapper.queryByCountingCode(countingCode);
	}

	/* @Title: querybarcodeExist 
	 * @Description: 根据条码，合同号到rawStorageSn中查询，判断当前条码是否在合同号下
	 * @author jzh
	 * @date 2018年5月29日 上午9:44:59 
	 */
	@Override
	public List<RawStorageSn> querybarcodeExist(RawStorageSn rawStorageSn) {
		
		return rawStorageSnMapper.queryByAny(rawStorageSn);
	}

	/* @Title: createRawCountingRecords 
	 * @Description: 创建盘点记录
	 * @author jzh
	 * @date 2018年5月29日 上午10:27:26 
	 */
	@Override
	public Resp createRawCountingRecords(RawCountingRecords rawCountingRecords) {
		rawCountingRecordsMapper.create(rawCountingRecords);
		return success();
	}

	/* @Title: queryRawCountingRecordsExist 
	 * @Description: 查看这个条码是否已经被盘点了countingCode barcode
	 * @author jzh
	 * @date 2018年5月29日 上午10:47:36 
	 */
	@Override
	public List<RawCountingRecords> queryRawCountingRecordsExist(RawCountingRecords rawCountingRecords) {
		
		return rawCountingRecordsMapper.queryByAny(rawCountingRecords);
	}

	@Override
	public int querCountByCountingCode(String countingCode) {
		RawCountingRecords rawCountingRecords = new RawCountingRecords();
		rawCountingRecords.setCountingCode(countingCode);
		
		return rawCountingRecordsMapper.queryCountAny(rawCountingRecords);
	}

	/* @Title: queryLose 
	 * @Description: 根据盘点单号，合同号 查询盘亏数据
	 * @author jzh
	 * @date 2018年5月30日 上午11:09:39 
	 */
	@Override
	public List<RawStorageSn> queryLose(RawCountingDto rawCountingDto) {
		
		return rawStorageSnMapper.queryLose(rawCountingDto);
	}

	/* @Title: createRawCountingDifference 
	 * @Description: 创建盘点差异 
	 * @author jzh
	 * @date 2018年5月30日 下午1:28:01 
	 */
	@Override
	public Resp createRawCountingDifference(RawCountingDifference rawCountingDifference) {
		rawCountingDifferenceMapper.create(rawCountingDifference);
		return success();
	}

	/* @Title: queryWin 
	 * @Description: 根据盘点单号，合同号 查询盘赢数据
	 * @author jzh
	 * @date 2018年5月30日 下午2:05:59 
	 */
	@Override
	public List<RawCountingRecords> queryWin(RawCountingDto rawCountingDto) {
		// TODO Auto-generated method stub
		return rawCountingRecordsMapper.queryWin(rawCountingDto);
	}

	/* @Title: updateRawCountingTaskFinish 
	 * @Description: 将盘点任务状态置为盘点结束
	 * @author jzh
	 * @date 2018年6月1日 上午10:36:19 
	 */
	@Override
	public Resp updateRawCountingTaskFinish(RawCountingTask rawCountingTask) {
		rawCountingTask.setStauts("2");
		Integer updateCnt = rawCountingTaskMapper.update(rawCountingTask);
		if(updateCnt.intValue() == 1){
			return success();
		}else{
			return fail("更改状态失败");
		}
	}

	/**
	 * @Title findRawStorageSnAbs
	 * @Description 查询库存明细列表（数量取绝对值）
	 * @author luoteng
	 * @date 2018年6月4日:下午5:28:41
	 * @param rawStorageSn
	 * @return List<RawStorageSn>
	 */
	@Override
	public List<RawStorageSn> findRawStorageSnAbs(RawStorageSn rawStorageSn){
		return rawStorageSnMapper.queryByAnyAbs(rawStorageSn);
	}

	@Override
	public Integer updateFlagByCode(RawStorageSn storageSn) {
		return rawStorageSnMapper.updateFlagByCode(storageSn);
	}

	@Override
	public Resp updateRawStorageSnWl(RawStorageSn rawStorageSn) {
		
		Integer updateCnt = rawStorageSnMapper.updateWlByCode(rawStorageSn);
		if(updateCnt.intValue() == 1){
			return success();
		}else{
			return fail("更改状态失败");
		}
	}

	@Override
	public Resp updateRawStorageSn(RawStorageSn rawStorageSn) {
		Integer updateCnt = rawStorageSnMapper.updateBySelect(rawStorageSn);
		if(updateCnt.intValue() == 1){
			return success();
		}else{
			return fail("更改状态失败");
		}
	}
	
	/**
	 * @Title: queryPlanList
	 * @Description: 原材料合同变更计划查询
	 * @date 2019年3月7日 上午10:47:10
	 * @author luoteng
	 * @param condition
	 * @return List<RawContractChangePlan>   
	 */
	@Override
	public List<RawContractChangePlan> queryPlanList(RawContractChangePlan condition){
		return rawContractChangePlanMapper.queryByAny(condition);
	}
	
	/**
	 * @Title: planScanBarcode
	 * @Description: 原材料合同变更扫码
	 * @date 2019年3月7日 上午11:10:21
	 * @author luoteng
	 * @param rawContractChangePlanDto
	 * @return TransResult   
	 */
	@Transactional
	@Override
	public TransResult planScanBarcode(RawContractChangePlanDto rawContractChangePlanDto) {
		TransResult result = new TransResult();
		result.setCode(Constant.SUCCESS);

		Date now = new Date();

		RawStorageSn rawStorageSn = new RawStorageSn();
		rawStorageSn.setBarcode(rawContractChangePlanDto.getBarcode());
		rawStorageSn.setActiveFlag("1");

		List<RawStorageSn> storageList = rawStorageSnMapper.queryByAny(rawStorageSn);
		if (storageList == null || storageList.isEmpty()) {
			result.setCode(Constant.FAIL);
			result.setMsg("该条码不存在！");
			return result;
		} else if (storageList.size() > 1) {
			result.setCode(Constant.FAIL);
			result.setMsg("该条码重复！");
			return result;
		} else {
			RawStorageSn storageSnTmp = storageList.get(0);

//        	0：可用 1：冻结 2：已出库 3：抽检冻结
			if (storageSnTmp.getBarcodeStatus() == null) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码状态为空！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("2")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码已出库！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("1") || storageSnTmp.getBarcodeStatus().equals("3")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码已冻结！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("0")) {
				RawContractChange rawContractChangePlanSn = new RawContractChange();
				rawContractChangePlanSn.setContractChangePlanNo(rawContractChangePlanDto.getContractChangePlanNo());
				rawContractChangePlanSn.setBarcode(rawContractChangePlanDto.getBarcode());
				rawContractChangePlanSn.setActiveFlag("1");

				List<RawContractChange> snList = rawContractChangeMapper.queryByAny(rawContractChangePlanSn);
				if (snList != null && !snList.isEmpty()) {
					result.setCode(Constant.FAIL);
					result.setMsg("该条码已经扫描，不能再次扫描!");
					return result;
				} else {
					RawContractChangePlan rawContractChangePlan = new RawContractChangePlan();
					rawContractChangePlan.setContractChangePlanNo(rawContractChangePlanDto.getContractChangePlanNo());
					rawContractChangePlan.setModel(storageSnTmp.getModel());
					rawContractChangePlan.setModelType(storageSnTmp.getModelType());
					rawContractChangePlan.setActiveFlag("1");

					List<RawContractChangePlan> planList = rawContractChangePlanMapper.queryByAny(rawContractChangePlan);
					if (planList == null || planList.isEmpty()) {
						result.setCode(Constant.FAIL);
						result.setMsg("该条码不是变更单所需型号!");
						return result;
					} else {
						RawContractChangePlan planTmp = planList.get(0);
						
//						变更类型：1：借出 2：归还
						String changeType = planTmp.getChangeType();
						
						if (!planTmp.getContractNoOld().equals(storageSnTmp.getContractNo())) {
							result.setCode(Constant.FAIL);
							result.setMsg("该条码合同号（" + storageSnTmp.getContractNo() + "）与该变更单所需合同号（"
									+ planTmp.getContractNoOld() + "）不相同!");
							return result;
						}
						List<RawContractChangePlan> rtnList = new ArrayList<>();

						// 需求数量
						BigDecimal requireAmount = planTmp.getRequireAmount() == null ? BigDecimal.ZERO
								: planTmp.getRequireAmount();
						// 已发数量
						BigDecimal realAmount = planTmp.getAmount() == null ? BigDecimal.ZERO
								: planTmp.getAmount();
						
						BigDecimal stgamount = storageSnTmp.getAmount() == null ? BigDecimal.ZERO : storageSnTmp.getAmount();

				        BigDecimal val = (realAmount.add(stgamount)).subtract(requireAmount);
				        int b = val.compareTo(Constant.RAW_MAX_STATIC_METER);
				        
				        Boolean typeFlag = isRawLength(storageSnTmp.getModelType());
						
						if (realAmount.compareTo(requireAmount) == 0 || requireAmount.compareTo(realAmount) < 0) {
							result.setCode(Constant.FAIL);
							result.setMsg("该型号已经扫码完成，无需继续扫码!");
							return result;
						}else if (typeFlag && b > 0) {
							result.setCode(Constant.FAIL);
							result.setMsg("该物料长度过长(实发数量只允许最多超过需求数量" + Constant.RAW_MAX_STATIC_METER + "米)!");
							return result;
						} else {
//							状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
							planTmp.setStatus("3");
							planTmp.setLastModifiedBy(rawContractChangePlanDto.getLoginName());
							planTmp.setGmtModified(now);
//							更新数量的sql 去掉Amout的更新
							planTmp.setAmount(null);
							planTmp.setAmountAdd(storageSnTmp.getAmount());
							
							realAmount = realAmount.add(storageSnTmp.getAmount());
						}

						Integer rtVal = rawContractChangePlanMapper.updateBySelect(planTmp);
						if (rtVal != 1) {
							throw new RuntimeException("扫描条码失败，更新条数存在问题!");
						}
						
						if("2".equals(changeType)) {
							RawContractChangePlan guihuan = new RawContractChangePlan();
							guihuan.setRawContractChangePlanId(planTmp.getRelationPlanId());
							
							List<RawContractChangePlan> tmpLLList = rawContractChangePlanMapper.queryByAny(guihuan);
							if (planList != null && !planList.isEmpty()) {
								RawContractChangePlan tmpYZOb = tmpLLList.get(0);
								
								// 借出数量
								BigDecimal amount = tmpYZOb.getAmount() == null ? BigDecimal.ZERO
										: tmpYZOb.getAmount();
								
								// 归还数量
								BigDecimal backAmount = tmpYZOb.getBackAmount() == null ? BigDecimal.ZERO
										: tmpYZOb.getBackAmount();
								backAmount = backAmount.add(storageSnTmp.getAmount());
								
								if (backAmount.compareTo(amount) == 0) {
//									归还状态： 0：未归还 1：部分归还 2：已经归还
									guihuan.setBackStatus("2");
								}else {
									guihuan.setBackStatus("1");
								}
							}
							
							guihuan.setBackAmountAdd(storageSnTmp.getAmount());
							
//							归还扫码时，关联的借出计划里面退回数量+1
							rawContractChangePlanMapper.updateBySelect(guihuan);
						}

//						手持显示 该条码长度
						planTmp.setAmount(storageSnTmp.getAmount());
//						返回给手持数据
						rtnList.add(planTmp);
						result.setList(rtnList);

//						RawContractChangePlan yzOb = new RawContractChangePlan();
//						yzOb.setRawContractChangePlanId(planTmp.getRawContractChangePlanId());
//						List<RawContractChangePlan> yzList = rawContractChangePlanMapper.queryByAny(yzOb);
//						if(yzList != null && !yzList.isEmpty()) {
//							// 需求数量
//							requireAmount = planTmp.getRequireAmount() == null ? BigDecimal.ZERO
//									: planTmp.getRequireAmount();
//							// 已发数量
//							realAmount = planTmp.getAmount() == null ? BigDecimal.ZERO
//									: planTmp.getAmount();
//							if (requireAmount.compareTo(realAmount) < 0) {
//								throw new RuntimeException("该型号已经扫码完成，无需继续扫码!");
//							}
//						}

						RawContractChange snInsertOb = new RawContractChange();
						snInsertOb.setContractChangePlanId(planTmp.getRawContractChangePlanId());
						snInsertOb.setContractChangePlanNo(planTmp.getContractChangePlanNo());
						snInsertOb.setContractNoNew(planTmp.getContractNoNew());
						snInsertOb.setContractNoOld(planTmp.getContractNoOld());
						snInsertOb.setBarcode(rawContractChangePlanDto.getBarcode());
						snInsertOb.setModel(planTmp.getModel());
						snInsertOb.setModelDesc(planTmp.getModelDesc());
						snInsertOb.setModelType(planTmp.getModelType());
						snInsertOb.setType(planTmp.getType());
						snInsertOb.setAmount(storageSnTmp.getAmount());
						snInsertOb.setUnit(storageSnTmp.getUnit());
						snInsertOb.setWlCode(storageSnTmp.getWlCode());
						snInsertOb.setActiveFlag("1");
						snInsertOb.setGmtCreate(now);
						snInsertOb.setCreateBy(rawContractChangePlanDto.getLoginName());

						rawContractChangeMapper.create(snInsertOb);

//						List<ProHandInSn> handInSnYzList = proHandInSnMapper.queryByAny(proHandInSn);
//						if(handInSnYzList.size() > 1){
//							throw new RuntimeException();
//						}

						storageSnTmp.setContractNo(planTmp.getContractNoNew());
						storageSnTmp.setLastModifiedBy(rawContractChangePlanDto.getLoginName());
						storageSnTmp.setGmtModified(now);

						rawStorageSnMapper.updateBySelect(storageSnTmp);
					}
				}

			}
		}

		return result;
	}
	
	/**
	 * @Title: planDelBarcode
	 * @Description: 原材料合同变更退扫
	 * @date 2019年3月7日 上午11:13:33
	 * @author luoteng
	 * @param rawContractChangePlanDto
	 * @return TransResult   
	 */
	@Transactional
	@Override
	public TransResult planDelBarcode(RawContractChangePlanDto rawContractChangePlanDto) {
		TransResult result = new TransResult();
		result.setCode(Constant.SUCCESS);

		Date now = new Date();

		RawStorageSn RawStorageSn = new RawStorageSn();
		RawStorageSn.setBarcode(rawContractChangePlanDto.getBarcode());
		RawStorageSn.setActiveFlag("1");

		List<RawStorageSn> storageList = rawStorageSnMapper.queryByAny(RawStorageSn);
		if (storageList == null || storageList.isEmpty()) {
			result.setCode(Constant.FAIL);
			result.setMsg("该条码不存在！");
			return result;
		} else if (storageList.size() > 1) {
			result.setCode(Constant.FAIL);
			result.setMsg("该条码重复!");
			return result;
		} else {
			RawStorageSn storageSnTmp = storageList.get(0);
			
			if (storageSnTmp.getBarcodeStatus() == null) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码状态为空！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("2")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码已出库！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("1") || storageSnTmp.getBarcodeStatus().equals("3")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码已冻结！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("0")) {
				RawContractChange planSn = new RawContractChange();
				planSn.setContractChangePlanNo(rawContractChangePlanDto.getContractChangePlanNo());
				planSn.setBarcode(rawContractChangePlanDto.getBarcode());
				planSn.setActiveFlag("1");

				List<RawContractChange> snList = rawContractChangeMapper.queryByAny(planSn);

				if (snList == null || snList.isEmpty()) {
					result.setCode(Constant.FAIL);
					result.setMsg("该条码未扫描!");
					return result;
				} else {
					RawContractChange snTmp = snList.get(0);

					RawContractChangePlan rawContractChangePlan = new RawContractChangePlan();
					rawContractChangePlan.setContractChangePlanNo(rawContractChangePlanDto.getContractChangePlanNo());
					rawContractChangePlan.setModel(storageSnTmp.getModel());
					rawContractChangePlan.setModelType(storageSnTmp.getModelType());
					rawContractChangePlan.setActiveFlag("1");

					List<RawContractChangePlan> planList = rawContractChangePlanMapper.queryByAny(rawContractChangePlan);
					if (planList == null || planList.isEmpty()) {
						result.setCode(Constant.FAIL);
						result.setMsg("条码型号与变更单所需型号不一致!");
						return result;
					} else {
						RawContractChangePlan planTmp = planList.get(0);
						
//						变更类型：1：借出 2：归还
						String changeType = planTmp.getChangeType();
						
						List<RawContractChangePlan> rtnList = new ArrayList<>();

						// 需求数量
						BigDecimal requireAmount = planTmp.getRequireAmount() == null ? BigDecimal.ZERO
								: planTmp.getRequireAmount();
						// 已发数量
						BigDecimal realAmount = planTmp.getAmount() == null ? BigDecimal.ZERO
								: planTmp.getAmount();
						
						
//						状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
						planTmp.setStatus("3");
						planTmp.setLastModifiedBy(rawContractChangePlanDto.getLoginName());
						planTmp.setGmtModified(now);
//						更新数量的sql 去掉Amout的更新
						planTmp.setAmount(null);

						planTmp.setAmountDel(storageSnTmp.getAmount());
						realAmount = realAmount.subtract(storageSnTmp.getAmount());

						Integer rtVal = rawContractChangePlanMapper.updateBySelect(planTmp);
						if (rtVal != 1) {
							throw new RuntimeException("删除条码失败，更新条数存在问题!");
						}
						
						if("2".equals(changeType)) {
							RawContractChangePlan guihuan = new RawContractChangePlan();
							guihuan.setRawContractChangePlanId(planTmp.getRelationPlanId());
							
							List<RawContractChangePlan> tmpLLList = rawContractChangePlanMapper.queryByAny(guihuan);
							if (planList != null && !planList.isEmpty()) {
								RawContractChangePlan tmpYZOb = tmpLLList.get(0);
								
								// 归还数量
								BigDecimal backAmount = tmpYZOb.getBackAmount() == null ? BigDecimal.ZERO
										: tmpYZOb.getBackAmount();
								backAmount = backAmount.subtract(new BigDecimal(1));
								
								if (backAmount.compareTo(BigDecimal.ZERO) == 0) {
//									归还状态： 0：未归还 1：部分归还 2：已经归还
									guihuan.setBackStatus("0");
								}else {
									guihuan.setBackStatus("1");
								}
							}
							
							guihuan.setBackAmountDel(storageSnTmp.getAmount());
							
//							归还退扫时，关联的借出计划里面退回数量-1
							rawContractChangePlanMapper.updateBySelect(guihuan);
						}

//						手持显示 该条码长度
						planTmp.setAmount(storageSnTmp.getAmount());
//						返回给手持数据
						rtnList.add(planTmp);
						result.setList(rtnList);

						snTmp.setActiveFlag("0");
						snTmp.setLastModifiedBy(rawContractChangePlanDto.getLoginName());
						snTmp.setGmtModified(now);

						Integer rtVal1 = rawContractChangeMapper.updateBySelect(snTmp);
						if (rtVal1 != 1) {
							throw new RuntimeException("删除条码失败，更新条数存在问题!");
						}

						storageSnTmp.setContractNo(planTmp.getContractNoOld());
						storageSnTmp.setLastModifiedBy(rawContractChangePlanDto.getLoginName());
						storageSnTmp.setGmtModified(now);

						rawStorageSnMapper.updateBySelect(storageSnTmp);
					}
				}
			}
		}

		return result;
	}
	
	/**
	 * @Title: planComplete
	 * @Description: 原材料合同变更确认完成
	 * @date 2019年3月7日 上午11:16:02
	 * @author luoteng
	 * @param rawContractChangePlanDto
	 * @return TransResult   
	 */
	@Transactional
	@Override
	public TransResult planComplete(RawContractChangePlanDto rawContractChangePlanDto) {
		TransResult result = new TransResult();
		result.setCode(Constant.SUCCESS);

		Date now = new Date();
		
		RawContractChangePlan rawContractChangePlan = new RawContractChangePlan();
		rawContractChangePlan.setContractChangePlanNo(rawContractChangePlanDto.getContractChangePlanNo());
		rawContractChangePlan.setActiveFlag("1");

		List<RawContractChangePlan> planList = rawContractChangePlanMapper.queryByAny(rawContractChangePlan);
		if (planList == null || planList.isEmpty()) {
			result.setCode(Constant.FAIL);
			result.setMsg("该变更单不存在!");
			return result;
		} else {
			for (RawContractChangePlan tmp : planList) {
				// 需求数量
				BigDecimal requireAmount = tmp.getRequireAmount() == null ? BigDecimal.ZERO : tmp.getRequireAmount();
				// 已发数量
				BigDecimal realAmount = tmp.getAmount() == null ? BigDecimal.ZERO : tmp.getAmount();

//				BigDecimal maxRealAmout = Constant.PRO_JY_STATIC_METER.add(requireAmount);

				if (requireAmount.compareTo(realAmount) > 0) {
					result.setCode(Constant.FAIL);
					result.setMsg("该变更单未扫码完毕!");
					return result;
				} 
//				else if (maxRealAmout.compareTo(realAmount) < 0) {
//					result.setCode(Constant.FAIL);
//					result.setMsg("该变更单扫码数量超过需求数量!");
//					return result;
//				}
			}
			
			RawContractChangePlan upOb = new RawContractChangePlan();
			upOb.setContractChangePlanNo(rawContractChangePlanDto.getContractChangePlanNo());
//			状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
			upOb.setStatus("5");
			upOb.setLastModifiedBy(rawContractChangePlanDto.getLoginName());
			upOb.setGmtModified(now);

			rawContractChangePlanMapper.updateByPlanNo(upOb);
		}

		return result;
	}
}
